package me.zhengjie.inter.rest;

import io.minio.MinioClient;
import io.minio.PostPolicy;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.aop.annotation.BisLog;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.domain.LocalStorage;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.inter.service.MpZhsqService;
import me.zhengjie.service.LocalStorageService;
import me.zhengjie.service.dto.LocalStorageQueryCriteria;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.Map;

/**
 * @author xiahongbo
 * @date 2022-07-10
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "移动端附件维护")
@RequestMapping("/mp/localStorage")
public class MpLocalStorageController {

    private final LocalStorageService localStorageService;

    @ApiOperation("按照业务ID和附件类别查询")
    @GetMapping()
    public ResponseEntity<Object> queryFileByFjlb(LocalStorageQueryCriteria criteria){
        System.out.println(criteria);
        return new ResponseEntity<>(PageUtil.toMpContent(localStorageService.queryAll(criteria)), HttpStatus.OK);
    }
    @Log("minio上传预签名")
    @ApiOperation("minio上传预签名")
    @GetMapping("/presignedPostPolicy")
    public ResponseEntity<Object> presignedPostPolicy(String name,String type) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidExpiresRangeException {
        MinioClient minioClient = new MinioClient("http://120.92.159.128:9001", "ddappuser", "dd1234@1");
        PostPolicy policy = new PostPolicy("mypic", name,
                ZonedDateTime.now().plusDays(7));
        policy.setContentType(type);
        Map<String,String> formData = minioClient.presignedPostPolicy(policy);
        return new ResponseEntity<>(PageUtil.toMpContent(formData), HttpStatus.OK);
    }

    @Log("minio下载预签名")
    @ApiOperation("minio下载预签名")
    @GetMapping("/presignedGetObject")
    public ResponseEntity<Object> presignedGetObject(String name) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidExpiresRangeException {
        MinioClient minioClient = new MinioClient("http://120.92.159.128:9001", "ddappuser", "dd1234@1");
        String url = minioClient.presignedGetObject("mypic",name,24*60*60);
        return new ResponseEntity<>(PageUtil.toMpContent(url), HttpStatus.OK);
    }

    @Log("移动端新增文件记录")
    @ApiOperation("移动端新增文件记录")
    @PostMapping()
    public ResponseEntity<Object> uploadPicture(@Validated @RequestBody LocalStorage localStorage){
        String fileName = localStorage.getName();

        String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
        String type = FileUtil.getFileType(suffix);
        localStorage.setSuffix(suffix);
        localStorage.setType(type);

        return new ResponseEntity<>(PageUtil.toMpContent(localStorageService.create(localStorage)), HttpStatus.OK);
    }


    @Log("删除文件")
    @DeleteMapping
    @ApiOperation("多选删除")
    public ResponseEntity<Object> deleteFile(@RequestBody Long[] ids) {
        localStorageService.deleteAll(ids);
        return new ResponseEntity<>(PageUtil.toMpContent(ids),HttpStatus.OK);
    }
}
